package cn.wolfcode.luowowo.website.controller;


import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class SearchController {

    @Reference
    private IStrategySearchService strategySearchService;
    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private ITravelSearchService travelSearchService;
    @Reference
    private IUserInfoSearchService userInfoSearchService;
    @Reference
    private ISerachService serachService;

    @RequestMapping("q")
    public String query(Model model, @ModelAttribute("qo")SearchQueryObject qo){
        switch (qo.getType()){
            case 0://目的地
                return  destSearch(model,qo);
            case 1://攻略
                 return  strategySearch(model,qo);
            case 2://游记
                 return  travel(model,qo);
            case 3://用户
                return  UserSearch(model,qo);
            default://全部
                return allSearch(model, qo);
        }
    }
    public String destSearch (Model model,@ModelAttribute("qo")SearchQueryObject qo){
        //根据关键字来查询目的地的
        DestinationTemplate dest = destinationSearchService.queryDestByName(qo.getKeyword());

        if(dest !=null){
            //如果存在,查询该目的地下所有的攻略,游记,用户
            //每页显示5条
            qo.setPageSize(5);
            qo.setDestId(dest.getId());

            //攻略
            Page<StrategyTemplate> strategy = strategySearchService.queryDestById(qo);
            //游记
            Page<TravelTemplate> travels = travelSearchService.query(qo);
            //用户
            Page<UserInfoTemplate> userInfo = userInfoSearchService.query(dest.getName());

            //data
            SearchResultVO vo = new SearchResultVO();
            vo.setStrategys(strategy.getContent());
            vo.setTravels(travels.getContent());
            vo.setUsers(userInfo.getContent());

            Long total = strategy.getTotalElements()+travels.getTotalElements()+userInfo.getTotalElements();
            vo.setTotal(total);
            //把所有页面上需要的值给传过去了
            model.addAttribute("data", vo);
        }
            model.addAttribute("dest", dest);


        return "/index/searchDest";
    }
    public String strategySearch (Model model,@ModelAttribute("qo")SearchQueryObject qo){
        //攻略
        Page<StrategyTemplate> strategy = serachService.searchWithHighlight(
                StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title","subTitle","summary");
        model.addAttribute("page",strategy);
        return "/index/searchStrategy";
    }
    public String travel (Model model,@ModelAttribute("qo")SearchQueryObject qo){
        //游记
        Page<TravelTemplate> travel = serachService.searchWithHighlight(
                TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title","summary");
        model.addAttribute("page", travel);
        return "/index/searchTravel";
    }
    public String UserSearch (Model model,@ModelAttribute("qo")SearchQueryObject qo){
        //用户
        Page<UserInfoTemplate> userInfo = serachService.searchWithHighlight(
                UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickName","destName");
        model.addAttribute("page", userInfo);
        return "/index/searchUser";
    }
    public String allSearch (Model model,@ModelAttribute("qo")SearchQueryObject qo){
        //目的
        Page<DestinationTemplate> destination = serachService.searchWithHighlight(
                DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME, DestinationTemplate.class, qo, "name","info");
        //攻略
        Page<StrategyTemplate> strategy = serachService.searchWithHighlight(
                StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title","subTitle","summary");
        //游记
        Page<TravelTemplate> travels = serachService.searchWithHighlight(
                TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title","summary");
        //用户
        Page<UserInfoTemplate> userInfo = serachService.searchWithHighlight(
                UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickName","destName");

        //data
        SearchResultVO vo = new SearchResultVO();
        vo.setStrategys(strategy.getContent());
        vo.setTravels(travels.getContent());
        vo.setUsers(userInfo.getContent());
        vo.setDests(destination.getContent());

        Long total = strategy.getTotalElements()+travels.getTotalElements()+userInfo.getTotalElements()+destination.getTotalElements();
        vo.setTotal(total);
        model.addAttribute("data", vo);
        return "/index/searchAll";
    }
}

